package com.qire.manhua.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.qire.common.collect.ArrayUtil;
import com.qire.manhua.R;

import androidx.appcompat.widget.TintTypedArray;

public class TagContainerLayout extends LinearLayout {

    private Drawable tagBackground;
    private int tagInterval = 0;
    private int tagPaddingLR = 0;
    private int tagPaddingTB = 0;
    private int tagTextSize;
    private int tagTextColor = Color.BLACK;

    public TagContainerLayout(Context context) {
        this(context, null);
    }

    public TagContainerLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TagContainerLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        this(context, attrs, defStyleAttr, 0);
    }

    @SuppressLint("RestrictedApi")
    public TagContainerLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        // 加载 TagContainerLayout 支持XML的属性集
        TintTypedArray tintTypedArray = TintTypedArray.obtainStyledAttributes(context, attrs, R.styleable.TagContainerLayout, defStyleAttr, 0);
        tagBackground = tintTypedArray.getDrawable(R.styleable.TagContainerLayout_tagBackground);
        tagInterval = tintTypedArray.getDimensionPixelSize(R.styleable.TagContainerLayout_tagInterval, 0);
        tagPaddingLR = tintTypedArray.getDimensionPixelSize(R.styleable.TagContainerLayout_tagPaddingLR, 0);
        tagPaddingTB = tintTypedArray.getDimensionPixelSize(R.styleable.TagContainerLayout_tagPaddingTB, 0);
        tagTextSize = tintTypedArray.getDimensionPixelSize(R.styleable.TagContainerLayout_tagTextSize, 20);
        tagTextColor = tintTypedArray.getColor(R.styleable.TagContainerLayout_tagTextColor, Color.BLACK);

    }


    public void setTextTags(CharSequence[] textTags) {
        if(ArrayUtil.isEmpty(textTags)) {
            return;
        }
        int textCount = textTags.length;
        int childCount = getChildCount();
        if(textCount < childCount) {
            removeViews(textCount, childCount - textCount);
        }

        for(int index = 0; index < textCount; index++) {

            if(index < childCount) {
                View view = getChildAt(index);
                if(view instanceof TextView) {
                    TextView textView = (TextView) view;
                    textView.setText(textTags[index]);
                } else {
                    removeView(view);
                    insertTagText(textTags[index], index);
                }
            } else {
                insertTagText(textTags[index], index);
            }
        }
    }

    private void insertTagText(CharSequence tagText, int index) {
        LayoutParams layoutParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        if (getOrientation() == VERTICAL) {
            layoutParams.topMargin = tagInterval;
            layoutParams.bottomMargin = tagInterval;
        } else {
            layoutParams.leftMargin = tagInterval;
            layoutParams.rightMargin = tagInterval;
        }
        TextView textView = new TextView(getContext());
        textView.setLayoutParams(layoutParams);
        textView.setGravity(Gravity.CENTER);
        textView.setPadding(tagPaddingLR, tagPaddingTB, tagPaddingLR, tagPaddingTB);
        textView.setText(tagText);
        textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, tagTextSize);
        textView.setTextColor(tagTextColor);
        textView.setBackground(tagBackground);
        addView(textView, index);
    }

}
